package com.hongyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.excel.utils.ExcelUtil;
import com.hongyi.common.framework.constant.UserConstans;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.system.domain.SysDictType;
import com.hongyi.system.domain.dto.SysDictTypeDto;
import com.hongyi.system.domain.vo.SysDictTypeVo;
import com.hongyi.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 数据字典类型
 *
 * @author DingHao
 */
@ApiSort(value = 9)
@Api(tags = "数据字典类型接口")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    /**
     * 服务对象
     */
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 分页查询字典类型列表
     */
    @SaCheckPermission("system:dict:list")
    @GetMapping("/list")
    public PageData<SysDictTypeVo> list(SysDictTypeDto sysDictTypeDto, PageQuery pageQuery) {
        return sysDictTypeService.selectPageDictTypeList(sysDictTypeDto, pageQuery);
    }

    /**
     * 查询字典类型详细导出(返回流)
     *
     * @param sysDictTypeDto 字典类型对象
     * @param response 返回
     */
    @ApiOperation(value = "查询字典类型详细导出(返回流)")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @ApiImplicitParam(name = "dictType", value = "字典类型对象", paramType = "query", dataTypeClass = SysDictType.class, required = true)
    @SaCheckPermission("system:dict:export")
    @PostMapping(value = "/exportByStream", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportByStream(SysDictTypeDto sysDictTypeDto, HttpServletResponse response) {
        List<SysDictTypeVo> sysDictTypeVos = sysDictTypeService.selectDictTypeList(sysDictTypeDto);
        ExcelUtil.exportExcel(sysDictTypeVos, "字典类型", SysDictTypeVo.class, response);
    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "字典类型列表")
    @ApiImplicitParam(name = "id", value = "字典类型ID", paramType = "path", dataTypeClass = Long.class, required = true)
    @SaCheckPermission("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public JsonResult<SysDictTypeVo> getInfo(@PathVariable Long dictId) {
        return JsonResult.ok(sysDictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增字典类型")
    @ApiImplicitParam(name = "dictType", value = "字典类型对象", paramType = "query", dataTypeClass = SysDictTypeDto.class, required = true)
    @SaCheckPermission("system:dict:add")
    @Log(title = "新增字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public JsonResult<Void> add(@Validated @RequestBody SysDictTypeDto sysDictTypeDto) {
        if (UserConstans.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictTypeDto))) {
            return JsonResult.fail("新增字典'" + sysDictTypeDto.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.insertDictType(sysDictTypeDto);
        return JsonResult.ok();
    }

    /**
     * 修改字典类型
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改字典类型")
    @ApiImplicitParam(name = "dict", value = "字典类型对象", paramType = "query", dataTypeClass = SysDictType.class, required = true)
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public JsonResult<Void> edit(@Validated @RequestBody SysDictTypeDto sysDictTypeDto) {
        if (UserConstans.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictTypeDto))) {
            return JsonResult.fail("修改字典'" + sysDictTypeDto.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.updateDictType(sysDictTypeDto);
        return JsonResult.ok();
    }

    /**
     * 删除字典类型
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParam(name = "ids", value = "字典类型ID数组", paramType = "path", dataTypeClass = Long.class, allowMultiple = true, required = true)
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public JsonResult<Void> remove(@PathVariable Long[] ids) {
        sysDictTypeService.deleteDictTypeByIds(ids);
        return JsonResult.ok();
    }

    /**
     * 清空缓存
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "字典类型清空缓存")
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping(value = "/clearCache")
    public JsonResult<String> clearCache() {
        sysDictTypeService.clearCache();
        return JsonResult.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "获取字典选择框列表")
    @GetMapping(value = "/optionselect")
    public JsonResult<List<SysDictTypeVo>> optionSelect() {
        List<SysDictTypeVo> dictTypes = sysDictTypeService.selectDictTypeAll();
        return JsonResult.ok(dictTypes);
    }

}
